<?php
/**
 * Created by PhpStorm.
 * User: yys
 * Date: 2016/12/2
 * Time: 13:39
 */

namespace App\Services\SourcePicService;

use App\Services\ImgServices\ImgTool;
use App\Services\Tool\GeneralTool;
use App\Services\Tool\Jpush\JpushTool;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Validator;

class SourcePicFrontService
{
    protected $imgTool;
    /**
     * SourcePicFrontService constructor.
     */
    public function __construct(ImgTool $imgTool)
    {
        $this->imgTool=$imgTool;
    }

    /***
     * 上传简单素材信息
     * @param array $data
     * @return bool
     */
    public function uploadSourcePicSimpleInfo(array $data){
        try{
            $image=$data['file'];
            unset($data['file']);
            if(!empty($image)){
                $img_path=$this->imgTool->imgSave($image,2);
            }else{
                return false;
            }
            if(empty($img_path)){
                return false;
            }
            if(!empty($img_path)){
                $data['original_image']=$img_path;
                $image_thumbnail_url=$this->imgTool->getImgThumbByImgPath($img_path,600,600,true);
                $image_small_url=$this->imgTool->getImgThumbByImgPath($img_path,300,300,true);
                $data['thumbnail']=$image_thumbnail_url;
                $data['small_image']=$image_small_url;
            }else{
                return false;
            }
            $source_pic_id=GeneralTool::only_no();
            $data['source_pic_id']=$source_pic_id;
            $res=self::saveSourcePicDB($data);
            return ($res)?$source_pic_id:false;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }
    /***
     * 保存素材数据
     * @param array $data
     * @return bool
     */
    private function saveSourcePicDB(array $data){
        try{
            $data['state']=1;
            $data['created_at']=time();
            $data['updated_at']=time();
            DB::beginTransaction();
            $res=DB::table('source_pics')->insert($data);
            return ($res==1)?true:false;
        }catch (\Exception $e){
            DB::rollback();
            Log::info($e);
            return false;
        }finally{
            DB::commit();
        }
    }
    /***
     * 图片上传校验
     * @param $data
     * @return array
     */
    public function imgSaveValidator($data){
        $rules=array(
            'file' => 'required|mimes:png,gif,jpeg,jpg,bmp',
        );
        $validator = Validator::make($data, $rules);
        if ($validator->fails()) {
            if ($validator->errors()->first('file')) {
                return GeneralTool::createResult('', 'M00000');
            }else{
                return GeneralTool::createResult('', 'M00001');
            }
        }
    }

    /***
     * 获取素材列表
     * @param array $data
     * @return mixed
     */
    public function getSourcePicList(array $data)
    {
        $user_id=$data['user_id'];
        $per_page=$data['per_page'];
        $is_shelves=$data['is_shelves'];
        $audit_state=$data['audit_state'];
        $query=DB::table('source_pics')->where(array('state'=>1,'user_id'=>$user_id));
        if($is_shelves==0||$is_shelves==1){
            $query->where('is_shelves',$is_shelves);
        }
        if($audit_state!=-1){
            $query->where('audit_state',$audit_state);
        }
        $source_pics=$query->orderBy('updated_at','DESC') ->paginate($per_page);
        return $source_pics;
    }

    /***
     * 修改素材价格
     * @param $source_pic_id
     * @param $price
     * @return bool
     */
    public function updatePrice($source_pic_id,$price){

        $res=DB::table('source_pics')->where(array('state'=>1,'source_pic_id'=>$source_pic_id))
            ->update(array(
                'price'=>$price,
                'updated_at'=>time(),
            ));
        return ($res==1)?true:false;
    }

    /***
     * 获取单条素材信息
     * @param $source_pic_id
     * @return null
     */
    public  static function getSourcePic($source_pic_id,$state=1)
    {
        $source_pic_id=trim($source_pic_id);
        if(empty($source_pic_id)){
            return null;
        }
        $query=DB::table('source_pics')->where(array('source_pic_id'=>$source_pic_id));
        if($state===1){
            $query->where('state',1);
        }
        $source_pic=$query->first();
        return $source_pic;
    }

    /***
     * 素材更新
     * @param array $data
     * @return bool
     */
    public function updateSourcePic(array $data){
        try{
            unset($data['limited_time']);
            unset($data['end_time_str']);
            $data['updated_at']=time();
            $source_pic_id=$data['source_pic_id'];
            $query=DB::table('source_pics')->where(array('source_pic_id'=>$source_pic_id,'state'=>1));
            $num=$query->update($data);
            return ($num==0)?false:true;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }

    /***
     * 更新素材名称
     * @param $source_pic_id
     * @param $source_pic_name
     * @return bool
     */
    public function updateSourcePicName($source_pic_id,$source_pic_name){
        try{
            $num=DB::table('source_pics')->where(array('source_pic_id'=>$source_pic_id,'state'=>1))
                ->update(array(
                    'name'=>$source_pic_name,
                    'updated_at'=>time()
                ));
            return ($num==0)?false:true;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }
    public function sourcePicPush($source_pic,$user_id){
        try{
            $user_id=$user_id.'';
            if(empty($source_pic)){
                return false;
            }
            if($source_pic->price===0){
                $price='免费';
            }else{
                $price='¥'.GeneralTool::priceFormat($source_pic->price);
            }
            $message='出售价格:'.$price.'\n';
            if($source_pic->end_time==0){
                $message.='下架时间:暂无';
            }else{
                $message.='下架时间:'.date('Y年m月d日 H:i',$source_pic->end_time);
            }
            JpushTool::push(
                '【素材审核中】你有⼀个素材素材审核中>>查看详情',
                0,
                $user_id,
                ['title'=>'素材审核中',
                    'user_id'=>$user_id,
                    'class'=>'system_notification',
                    'type'=>1,
                    'jump_value'=>'',
                    'template_no'=>'4',
                    'message'=>$message,
                    'images'=>
                        [
                            ['original_image'=>$source_pic->original_image,'thumbnail'=>$source_pic->small_image]
                        ]
                ]);
            return true;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }
    /***
     * 素材删除
     * @param $source_pic_id
     * @return bool
     */
    public function delSourcePic($source_pic_id){
        try{
            $num=DB::table('source_pics')->where(array('source_pic_id'=>$source_pic_id,'state'=>1))
                ->update(array(
                    'state'=>0,
                    'updated_at'=>time(),
                ));
            return ($num==0)?false:true;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }
}